#include <iostream>
#include <vector>
#include <string>
#include <climits>

using namespace std;

int get_min(string& str1, string& str2, vector<string> strs)
{
	int ret = INT_MAX;
	int index1 = -1, index2 = -1;
	for (int i = 0; i < strs.size(); ++i)
	{
		if (strs[i] == str1)
			index1 = i;
		if (str[i] == str2)
			index2 = i;
		
		if (index1 >= 0 && index2 >= 0)
			ret = min(ret, abs(index1 - index2));
	}
	
	return ret > strs.size() ? -1 : ret;
}

int main()
{
	int n;
	cin >> n;
	string str1, str2;
	cin >> str1 >> str2;
	
	vector<string> strs(n);
	for (auto& i : strs)
		cin >> i;
	
	cout << get_min(str1, str2, strs) << endl;
		
	return 0;
}